package com.zzvcom.office.controllers;

import com.zzvcom.office.entities.OfficeFile;
import com.zzvcom.office.exception.OfficeException;
import com.zzvcom.office.services.DownloadServices;
import com.zzvcom.office.util.LimitUtil;
import com.zzvcom.office.vo.FilePdfVo;
import com.zzvcom.office.vo.OfficeFileResultVo;
import com.zzvcom.office.vo.ResultVo;
import jakarta.validation.constraints.NotBlank;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Controller
@Slf4j
@CrossOrigin("*")
@RequestMapping("/download")
@Validated
public class DownloadController {


    @Autowired
    private DownloadServices downloadServices;

    @Autowired
    private LimitUtil limitUtil;

    private static String makeEditorPath(String action, String type, OfficeFile officeFile) {
        return "redirect:/editor?fileId=" + officeFile.getId() + "&action=" + action + "&type=" + type;
        //return "forward:/editor?fileId=" + officeFile.getId() + "&action=" + action + "&type=" + type;
    }

    /**
     * 给出url进行浏览，使用only进行浏览或者编辑
     * 如果是doc，docx,ppt->pptx,xsl->xslx要进行转码
     * http://192.168.175.189:4000/tryoffice/download/view?url=https%3A%2F%2Fstoredt.czbanbantong.com%2Fdocum%2Frms%2Fnrms%2F20240730%2F1722333551196.pptx%3Fct%3D1%26title%3D%25E3%2580%258Adtnl%25EF%25BC%2588%25E7%25AC%25AC%25E4%25BA%258C%25E8%25AF%25BE%25E6%2597%25B6%25EF%25BC%2589%25E3%2580%258B%25E7%25BB%259F%25E7%25BC%2596%25E7%2589%2588%25E4%25B8%2580%25E5%25B9%25B4%25E7%25BA%25A7%25E4%25B8%258A%25E5%2586%258C%25E8%258C%2583%25E7%2591%25B6%25E5%25A4%25A7%25E5%2590%258C%25E5%25B8%2582%25E5%25B9%25B3%25E5%259F%258E%25E5%258C%25BA%25E5%258D%2581%25E5%259B%259B%25E6%25A0%25A1%25E5%258D%2581%25E9%2587%258C%25E5%25BA%2597%25E5%2588%2586%25E6%25A0%25A1.pptx
     *
     * @param url    http://localhost:4000/tryoffice/download/view?url=https%3A%2F%2Fstoredt.czbanbantong.com%2Fdocum%2Frms%2Fnrms%2F20240730%2F1722333551196.pptx%3Fct%3D1%26title%3D%25E3%2580%258Adtnl%25EF%25BC%2588%25E7%25AC%25AC%25E4%25BA%258C%25E8%25AF%25BE%25E6%2597%25B6%25EF%25BC%2589%25E3%2580%258B%25E7%25BB%259F%25E7%25BC%2596%25E7%2589%2588%25E4%25B8%2580%25E5%25B9%25B4%25E7%25BA%25A7%25E4%25B8%258A%25E5%2586%258C%25E8%258C%2583%25E7%2591%25B6%25E5%25A4%25A7%25E5%2590%258C%25E5%25B8%2582%25E5%25B9%25B3%25E5%259F%258E%25E5%258C%25BA%25E5%258D%2581%25E5%259B%259B%25E6%25A0%25A1%25E5%258D%2581%25E9%2587%258C%25E5%25BA%2597%25E5%2588%2586%25E6%25A0%25A1.pptx
     *               http://localhost:4000/tryoffice/download/view?url=https%3A%2F%2Fstore.youxuegaoshou.com%2Fdocum%2Frms%2Fnrms%2F20250804%2F1754284780881.doc%3Fct%3D1%26title%3D%25E7%25AC%25AC%25E4%25BA%258C%25E5%2591%25A8%25E4%25BD%259C%25E4%25B8%259A%25E7%25AD%2594%25E6%25A1%2588.doc
     *               http://localhost:4000/tryoffice/download/view?url=https%3A%2F%2Fncp.czbanbantong.com%2Fqgoss%2Fdocum%2Fonedrive%2Fncp.czbanbantong.com%2F20250711%2F688138784752925020.docx%3Fct%3D1%26title%3D3%25E6%259A%2591B%25E7%25AC%25AC1%25E8%25AE%25B2%25E3%2580%258A%25E6%2595%25B0%25E6%258D%25AE%25E7%259A%2584%25E6%2594%25B6%25E9%259B%2586%25E4%25B8%258E%25E6%2595%25B4%25E7%2590%2586%25E5%258F%258A%25E9%2599%25A4%25E6%25B3%2595%25E7%259A%2584%25E5%2588%259D%25E6%25AD%25A5%25E8%25AE%25A4%25E8%25AF%2586%25E3%2580%258B_1.docx
     *               <p>
     *               下载编辑的文件
     * @param action view edit
     * @param type   desktop  mobile  embedded
     */
    @GetMapping("/view")
    public String view(@NotBlank(message = "地址不能为空") String url,
                       @RequestParam(value = "action", required = false, defaultValue = "view") String action,
                       @RequestParam(value = "type", required = false, defaultValue = "embedded") final String type,
                       final Model model) throws Exception {
        url=limitUtil.validateUrl(url);

        OfficeFileResultVo officeFileResultVo = downloadServices.useOnlyOfficeByDownload(url);
        if (officeFileResultVo.isError()) {
            model.addAttribute("error", officeFileResultVo.getMessage());
            return "error.html";
        }
        OfficeFile officeFile = officeFileResultVo.getOfficeFile();
        if (officeFile != null) {
            return makeEditorPath(action, type, officeFile);
        }
        throw new OfficeException("officeFile is null");
    }

    /**
     * http://192.168.175.189:4000/tryoffice/download/pdf?url=https%3A%2F%2Fstoredt.czbanbantong.com%2Fdocum%2Frms%2Fnrms%2F20240730%2F1722333551196.pptx%3Fct%3D1%26title%3D%25E3%2580%258Adtnl%25EF%25BC%2588%25E7%25AC%25AC%25E4%25BA%258C%25E8%25AF%25BE%25E6%2597%25B6%25EF%25BC%2589%25E3%2580%258B%25E7%25BB%259F%25E7%25BC%2596%25E7%2589%2588%25E4%25B8%2580%25E5%25B9%25B4%25E7%25BA%25A7%25E4%25B8%258A%25E5%2586%258C%25E8%258C%2583%25E7%2591%25B6%25E5%25A4%25A7%25E5%2590%258C%25E5%25B8%2582%25E5%25B9%25B3%25E5%259F%258E%25E5%258C%25BA%25E5%258D%2581%25E5%259B%259B%25E6%25A0%25A1%25E5%258D%2581%25E9%2587%258C%25E5%25BA%2597%25E5%2588%2586%25E6%25A0%25A1.pptx
     * http://localhost:4000/download/pdf?url=https%3A%2F%2Fstoredt.czbanbantong.com%2Fdocum%2Frms%2Fnrms%2F20240730%2F1722333551196.pptx%3Fct%3D1%26title%3D%25E3%2580%258Adtnl%25EF%25BC%2588%25E7%25AC%25AC%25E4%25BA%258C%25E8%25AF%25BE%25E6%2597%25B6%25EF%25BC%2589%25E3%2580%258B%25E7%25BB%259F%25E7%25BC%2596%25E7%2589%2588%25E4%25B8%2580%25E5%25B9%25B4%25E7%25BA%25A7%25E4%25B8%258A%25E5%2586%258C%25E8%258C%2583%25E7%2591%25B6%25E5%25A4%25A7%25E5%2590%258C%25E5%25B8%2582%25E5%25B9%25B3%25E5%259F%258E%25E5%258C%25BA%25E5%258D%2581%25E5%259B%259B%25E6%25A0%25A1%25E5%258D%2581%25E9%2587%258C%25E5%25BA%2597%25E5%2588%2586%25E6%25A0%25A1.pptx
     *
     * @param url
     * @return
     * @throws Exception
     */
    @GetMapping("/pdf")
    @ResponseBody
    public ResultVo<FilePdfVo> viewPdf(@NotBlank(message = "地址不能为空") String url) throws Exception {
       url= limitUtil.validateUrl(url);
        FilePdfVo pdfVo = getFilePdfVo(url);
        return ResultVo.valueOf("200", "success", pdfVo);
    }

    /**
     * http://192.168.175.189:4000/tryoffice/download/pdf_file?url=https%3A%2F%2Fstoredt.czbanbantong.com%2Fdocum%2Frms%2Fnrms%2F20240730%2F1722333551196.pptx%3Fct%3D1%26title%3D%25E3%2580%258Adtnl%25EF%25BC%2588%25E7%25AC%25AC%25E4%25BA%258C%25E8%25AF%25BE%25E6%2597%25B6%25EF%25BC%2589%25E3%2580%258B%25E7%25BB%259F%25E7%25BC%2596%25E7%2589%2588%25E4%25B8%2580%25E5%25B9%25B4%25E7%25BA%25A7%25E4%25B8%258A%25E5%2586%258C%25E8%258C%2583%25E7%2591%25B6%25E5%25A4%25A7%25E5%2590%258C%25E5%25B8%2582%25E5%25B9%25B3%25E5%259F%258E%25E5%258C%25BA%25E5%258D%2581%25E5%259B%259B%25E6%25A0%25A1%25E5%258D%2581%25E9%2587%258C%25E5%25BA%2597%25E5%2588%2586%25E6%25A0%25A1.pptx
     * https://vcomoffice.yjt361.com/tryoffice/download/pdf_file?url=https%3A%2F%2Fstoredt.czbanbantong.com%2Fdocum%2Frms%2Fnrms%2F20240730%2F1722333551196.pptx%3Fct%3D1%26title%3D%25E3%2580%258Adtnl%25EF%25BC%2588%25E7%25AC%25AC%25E4%25BA%258C%25E8%25AF%25BE%25E6%2597%25B6%25EF%25BC%2589%25E3%2580%258B%25E7%25BB%259F%25E7%25BC%2596%25E7%2589%2588%25E4%25B8%2580%25E5%25B9%25B4%25E7%25BA%25A7%25E4%25B8%258A%25E5%2586%258C%25E8%258C%2583%25E7%2591%25B6%25E5%25A4%25A7%25E5%2590%258C%25E5%25B8%2582%25E5%25B9%25B3%25E5%259F%258E%25E5%258C%25BA%25E5%258D%2581%25E5%259B%259B%25E6%25A0%25A1%25E5%258D%2581%25E9%2587%258C%25E5%25BA%2597%25E5%2588%2586%25E6%25A0%25A1.pptx
     * http://localhost:4000/tryoffice/download/pdf_file?url=https%3A%2F%2Fl1rss.czbanbantong.com%2Flocalpath%2Fservicecenter%2F20241017%2F20241017172606763685775084284%2F%25E6%2596%25B0%25E6%2595%2599%25E6%259D%2590%25E2%2580%2594%25E5%258D%2595%25E5%2585%2583%25E6%2595%2599%25E5%25AD%25A6%25E5%25BB%25BA%25E8%25AE%25AE.pptx%3Fct%3D1
     *
     * @param url
     * @param ct
     * @return
     * @throws Exception
     */
    @GetMapping("/pdf_file")
    @ResponseBody
    public ResponseEntity<Resource> viewPdfFile(@NotBlank(message = "地址不能为空") String url,
                                                @RequestParam(name = "ct", required = false, defaultValue = "0") int ct) throws Exception {
        url=limitUtil.validateUrl(url);
        FilePdfVo pdfVo = getFilePdfVo(url);
        return downloadServices.getResourceResponseEntity(ct, pdfVo.getOfficePdf());
    }

    private FilePdfVo getFilePdfVo(String url) throws Exception {
        OfficeFileResultVo officeFileResultVo = downloadServices.useOnlyOfficeByDownload(url);
        if (officeFileResultVo.isError()) {
            throw new OfficeException(officeFileResultVo.getMessage());
        }
        OfficeFile officeFile = officeFileResultVo.getOfficeFile();
        if (officeFile == null) {
            throw new OfficeException("文件沒有初始化");
        }
        FilePdfVo pdfVo = downloadServices.converterPdf(officeFile, url);
        return pdfVo;
    }

}
